<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Cqhperson extends Permissions
{
	//年龄
	function ageVerification($code){
	    $age_time = strtotime(substr($code, 6, 8));
	    if($age_time === false){
	        return false;
	    }
	    list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age_time)); 
	 
	    $now_time = strtotime("now");
	 
	    list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now_time));
	    $age = $y2 - $y1;
	    if((int)($m2.$d2) < (int)($m1.$d1)){
	        $age -= 1;
	    }
	    return $age; 
	}
    public function index()
    {
    	$list = db('population')->where(['isdel'=>1])->select();
    	foreach ($list as $key => &$v) {
            switch ($v['status']) {
                case '1':
                    $v['status'] = '已完成';
                    break;
                case '2':
                    $v['status'] = '已签';
                    break;
                case '3':
                    $v['status'] = '已联系';
                    break;
                case '4':
                    $v['status'] = '未联系';
                    break;
                
                default:
                    # code...
                    break;
            }
        $v['child'] = db('popinfo')->where(['isdel'=>1,'pid'=>$v['id']])->select();
        $louhao = explode(',',$v['louhao']);

            foreach ($louhao as &$vv) {
                $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
            }
            // dump($louhao);die;
            $v['louhao'] = implode(',',$louhao);


    	}
    	$this->assign('list',$list);
        return $this->fetch();
    }
    //
    
    //拆迁户档案
    public function cqhfiles(){
        
        	$pid = input('id');
    	$zhu = db('population')->where(['id'=>$pid])->find();
    	$zhu['chairen'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->count('id');
    	$zhu['chaicun'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'是'])->count('id');
    	$zhu['chaifei'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'否'])->count('id');
    	$list = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->select();
    	foreach ($list as $k => &$v) {
    		$v['age_xian'] = $this->ageVerification($v['idcar']);
    		if ($v['age_xian'] >= 60) {
    			$v['name'] = '<span style="color:red">'.$v['name'].'</span>';
    			$v['age_xian'] = '<span style="color:red">'.$v['age_xian'].'</span>';
    		}
    	}
    	if (request()->isAjax()) {
    		$post = input('post.');
            // dump($post);die;
    		$res = db('popinfo')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
    	}
    	$this->assign('zhu',$zhu);
    	$this->assign('list',$list);
    	return $this->fetch();
        
        
        
    }
    
    
    //协议补偿明细
        public function compensate()
		{
			$pid = input('id');
			$zhu = db('population')->where(['id' => $pid])->find();
			$zhu['chairen'] = db('popinfo')->where(['isdel' => 1, 'pid' => $pid])->count('id');
			$zhu['chaicun'] = db('popinfo')->where(['isdel' => 1, 'pid' => $pid, 'iscun' => '是'])->count('id');
			$zhu['chaifei'] = db('popinfo')->where(['isdel' => 1, 'pid' => $pid, 'iscun' => '否'])->count('id');
			$list = db('popinfo')->where(['isdel' => 1, 'pid' => $pid])->select();
			foreach ($list as $k => &$v) {
				$v['age_xian'] = $this->ageVerification($v['idcar']);
				if ($v['age_xian'] >= 60) {
					$v['name'] = '<span style="color:red">' . $v['name'] . '</span>';
					$v['age_xian'] = '<span style="color:red">' . $v['age_xian'] . '</span>';
				}
			}
			// 	if (request()->isAjax()) {
			// 		$post = input('post.');
			//         // dump($post);die;
			// 		$res = db('popinfo')->insert($post);
			//         if ($res) {
			//             return 200;
			//         }else{
			//             return 202;
			//         }
			//         return $arr;
			// 	}
			$list2 = db('dishang')->select();
			//	var_dump($list2);die("111111111111111111111");
			foreach ($list2 as $key => &$v) {
				$v['name'] = db('population')->where('id', $v['uid'])->value('name');
			}

            //地上物
			$dishangInfo = db('dishang')->where('uid',$pid)->find();
			$this->assign('dishang', $dishangInfo);

			//各项
			$gexiangInfo = db('gexiang')->where('uid',$pid)->find();
			$this->assign('gexiang', $gexiangInfo);

			//额外
			$ewaiInfo = db('ewai')->where('uid',$pid)->find();
			$this->assign('ewai', $ewaiInfo);

			$this->assign('zhu', $zhu);
			$this->assign('list', $list);
			$this->assign('list2', $list2);
			return $this->fetch();

		}
      
   
    //款项发放汇总
    public function collect(){
          	$pid = input('id');
    	$zhu = db('population')->where(['id'=>$pid])->find();
    	$zhu['chairen'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->count('id');
    	$zhu['chaicun'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'是'])->count('id');
    	$zhu['chaifei'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'否'])->count('id');
    	$list = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->select();
    	foreach ($list as $k => &$v) {
    		$v['age_xian'] = $this->ageVerification($v['idcar']);
    		if ($v['age_xian'] >= 60) {
    			$v['name'] = '<span style="color:red">'.$v['name'].'</span>';
    			$v['age_xian'] = '<span style="color:red">'.$v['age_xian'].'</span>';
    		}
    	}
    	if (request()->isAjax()) {
    		$post = input('post.');
            // dump($post);die;
    		$res = db('popinfo')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
    	}
    	$this->assign('zhu',$zhu);
    	$this->assign('list',$list);
    	return $this->fetch();
        
    }
 
 
    //户主档案
    public function info()
    {
    	$pid = input('id');
    	$zhu = db('population')->where(['id'=>$pid])->find();
    	$zhu['chairen'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->count('id');
    	$zhu['chaicun'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'是'])->count('id');
    	$zhu['chaifei'] = db('popinfo')->where(['isdel'=>1,'pid'=>$pid,'iscun'=>'否'])->count('id');
    	$list = db('popinfo')->where(['isdel'=>1,'pid'=>$pid])->select();
    	foreach ($list as $k => &$v) {
    		$v['age_xian'] = $this->ageVerification($v['idcar']);
    		if ($v['age_xian'] >= 60) {
    			$v['name'] = '<span style="color:red">'.$v['name'].'</span>';
    			$v['age_xian'] = '<span style="color:red">'.$v['age_xian'].'</span>';
    		}
    	}
    	if (request()->isAjax()) {
    		$post = input('post.');
            // dump($post);die;
    		$res = db('popinfo')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
    	}
    	$this->assign('zhu',$zhu);
    	$this->assign('list',$list);


		//补偿款情况 协议补偿款 回迁结算款 拆迁总金额 已发放金额
		$dsMoney = db('dishang')->where(['uid'=>$pid])->value('sum'); //地上
		$cqMoney = db('gexiang')->where(['uid'=>$pid])->value('sum'); //各项
		$ewMoney = db('ewai')->where(['uid'=>$pid])->value('sum'); //额外
		$xyMoney1 =  bcadd($dsMoney,$cqMoney,2);
		$xyMoney = bcadd($xyMoney1,$ewMoney,2);
		$hqMoney = db('hqjiesuan')->where(['uid'=>$pid])->value('jinesum');
		$yfMoney = db('kxffhz')->where(['pid'=>$pid])->sum('money');
		$cqAllMoney = bcadd($xyMoney,$hqMoney,2);
		$bcInfo = [
			'xyMoney' => $xyMoney?$xyMoney:0,//协议补偿款
			'hqMoney' => $hqMoney?$hqMoney:0,//回迁结算款
			'yfMoney' => $yfMoney?$yfMoney:0,//已发放金额
			'cqAllMoney' => $cqAllMoney?$cqAllMoney:0, //拆迁总金额
		];
		$this->assign('bcInfo',$bcInfo);

		//其它款项累计发放统计
		$gxInfo = db('gexiang')->where(['uid'=>$pid])->find(); //各项
		$this->assign('gxInfo',$gxInfo);


		$azInfo = db('xieyianzhi')->where(['pid'=>$pid])->find();
		$mj11 = bcadd($azInfo['ydazlmj1'],$azInfo['ydazlmj2'],2);
        $mj1 = bcadd($mj11,$azInfo['ydazlmj3'],2);
        $mj22 = bcadd($azInfo['pjmj1'],$azInfo['pjmj2'],2);
        $mj2 = bcadd($mj22,$azInfo['pjmj3'],2);
        $mj33 = bcadd($azInfo['bzfmj1'],$azInfo['bzfmj2'],2);
        $mj3 = bcadd($mj33,$azInfo['bzfmj3'],2);
        $mj44 = bcadd($azInfo['tcwmj1'],$azInfo['tcwmj2'],2);
        $mj4 = bcadd($mj44,$azInfo['tcwmj3'],2);
        $mj55 = bcadd($azInfo['dxmj1'],$azInfo['dxmj2'],2);
        $mj5 = bcadd($mj55,$azInfo['dxmj3'],2);

        $mj = [
            'mj1' => $mj1,
            'mj2' => $mj2,
            'mj3' => $mj3,
            'mj4' => $mj4,
            'mj5' => $mj5,
        ];
        $this->assign('mj',$mj);




    	return $this->fetch();
    }
    public function info_data()
    {
    	$list = db('popinfo')->where(['isdel'=>1])->select();
    	$arr = [
    		'code'=>0,
    		'msg'=>'',
    		'data'=>$list
    	];
    	return json($arr); 
    }
}
